package com.mage.quickstart.controller;


import com.mage.quickstart.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

// @Controller
// @ResponseBody

@RestController
@Slf4j
public class IndexController {

    @Autowired
    private Student student;

    @RequestMapping("")
    public String index(Integer id, String name) {// Model, HttpServletRequest跟Web环境有关
        log.info("id={}, name={}", id, name);
        log.info(student.toString());
        return "Hello SpringBoot!";
    }

    @RequestMapping(path = {"welcome", "index", "page/{page}/{size}", "page/{page}"})
    public String welcome(@PathVariable(required=false) Integer page,
                          @PathVariable(required=false) Integer size) {
        return "Welcome SpringBoot!";
    }

    @RequestMapping(path = "method", method = RequestMethod.POST)
    public String limitMethod() {
        return "Just allow post request!";
    }

    @RequestMapping(path = "noid", params = {"!id"})
    public String noIdParams(String name) {
        log.info("name={}", name);
        return "Method not allow id parameter!";
    }

    @RequestMapping(path = "headers", headers = "Content-Type=text/*")
    public String limitRequestHeader() {
        return "Method just allow content-type=text/*!";
    }

    @RequestMapping(path = "getHeaders")
    // public String getHeaders(HttpServletRequest request) {
    public String getHeaders(@RequestHeader String token) {
//        String token = request.getHeader("token");
        log.info("获取Header的token值：{}", token);
        return "success";
    }

    @RequestMapping(path = "getObject"
//            ,produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
//            consumes = MediaType.APPLICATION_JSON_VALUE
    )
    // 其中produces规定输出的数据格式，consumes处理提交的请求的数据格式
    public Student getStudent() {
        return student;
    }

    @RequestMapping(path = "check",
            produces = MediaType.TEXT_PLAIN_VALUE
    )
    public Student checkConsumes() {
        return student;
    }
}
